import DateUtil from "../engine/DateUtil";
import { HaoEncrypt } from "../engine/HaoEncrypt";
import HttpUtil from "../engine/HttpUtil";
import LocalStorage from "../engine/LocalStorage";
import { Logger } from "../engine/Logger";
import { Md5Helper } from "../engine/Md5Helper";
import UIManager from "../engine/UIManager";
import Uint8ArrayHelper from "../engine/Uint8ArrayHelper";
import VersionManager from "../engine/VersionManager";
import { ClubConfig } from "../game/config/ClubConfig";
import { ErrorCode } from "../game/config/ErrorCode";
import { ViewConfig } from "../game/config/ViewConfig";
import { AccountModel } from "../game/model/AccountModel";
import { UserModel } from "../game/model/UserModel";
import { UIHelper } from "../game/util/UIHelper";
import GameController from "../GameController";
import { NativeSdkHelper } from "../sdk/NativeSdkHelper";
import { ServerConfig } from "./ServerConfig";
import { SocketClient } from "./SocketClient";

export class HttpHelper {


    public static get_jhao(callback: Function) {
        // callback && callback(false)
        // return;
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getWebServerUrl() + "/login_jhao"
        let data: object = {
            "t": DateUtil.now(),
            "channel": NativeSdkHelper.channel,
            "v": VersionManager.instance.nowVersion
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        Logger.log("get_jhao=", data, url)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            if (isOK) {
                if (response) {
                    let code: number = response["code"];
                    if (code == ErrorCode.Code.Suc) {
                        let data: string = response["data"]
                        let jhaoStr: string = HaoEncrypt.decode(data)
                        interface json {
                            anroid_url: string;
                            ios_url: string;
                            force_update: boolean;
                            need_hotupdate: boolean
                        }
                        let objVersion: json = JSON.parse(jhaoStr);
                        Logger.log("jhao----2---", objVersion)
                        ServerConfig.initAccountServer(objVersion)
                        VersionManager.instance.apkStoreUrl = objVersion.anroid_url;
                        VersionManager.instance.iosStoreUrl = objVersion.ios_url;
                        let force_update: boolean = objVersion["force_update"]
                        let need_hotupdate: boolean = objVersion["need_hotupdate"];
                        if (force_update) {
                            VersionManager.instance.jumpToPackUpate()
                        } else {
                            callback(need_hotupdate)
                        }
                    } else {
                        callback(false)
                    }
                } else {

                }
            }
        }, JSON.stringify(param));
    }

    public static accountReg(account: string, password: string, callback: Function) {
        UIManager.instance.showLoadingIcon()
        LocalStorage.setItem("account", account)
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getAccountServerUrl() + "/register"

        let data: object = {
            "t": DateUtil.now(),
            "account": account,
            "password": password,
            "channel": NativeSdkHelper.channel,
            "os": cc.sys.os,
            "phone_type": NativeSdkHelper.getPhoneType(),
            "bundle_name": NativeSdkHelper.getBundleName(),
            "system_version": NativeSdkHelper.getSystemVersion(),
            "udid": NativeSdkHelper.getUDID()
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            UIManager.instance.hideLoadingIcon()
            if (isOK) {
                Logger.log("accountReg==", response)
                if (response) {
                    let code: number = response["code"];
                    if (code == ErrorCode.Code.Suc) {
                        let data: object = response["data"];
                        AccountModel.instance.account = data["account"]
                        AccountModel.instance.token = data["token"]
                        ServerConfig.initHallServer(data)
                        callback && callback(true)
                        SocketClient.login(true)
                    } else {
                        let msg: string = response["msg"]
                        UIHelper.showTip(msg)
                        callback && callback(false)
                    }
                } else {

                }
            }
        }, JSON.stringify(param));

    }

    public static account_auth(account: string, password: string) {
        UIManager.instance.showLoadingIcon()
        LocalStorage.setItem("account", account)
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getAccountServerUrl() + "/account_auth"
        let data: object = {
            "t": DateUtil.now(),
            "account": account,
            "password": password,
            "channel": NativeSdkHelper.channel,
            "os": cc.sys.os,
            "phone_type": NativeSdkHelper.getPhoneType(),
            "bundle_name": NativeSdkHelper.getBundleName(),
            "system_version": NativeSdkHelper.getSystemVersion(),
            "udid": NativeSdkHelper.getUDID()
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        Logger.log("account_auth=req=", data)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            // Logger.log("account_auth=req=222", response)
            UIManager.instance.hideLoadingIcon()
            if (isOK) {
                Logger.log("account_auth==", response)
                if (response) {
                    let code: number = response["code"];
                    if (code == ErrorCode.Code.Suc) {
                        let data: object = response["data"]
                        AccountModel.instance.account = data["account"]
                        AccountModel.instance.nickname = data["nickname"]
                        AccountModel.instance.password = password
                        AccountModel.instance.token = data["token"]
                        AccountModel.instance.save()
                        ServerConfig.initHallServer(data)
                        SocketClient.login(true)
                        // callback && callback(true)
                    } else {
                        let msg: string = response["msg"]
                        UIHelper.showTip(msg)
                        // callback && callback(false)
                    }
                } else {

                }
            }
        }, JSON.stringify(param));
    }

    public static wechat_auth(code: string) {
        UIManager.instance.showLoadingIcon()
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getAccountServerUrl() + "/wechat_auth"
        let data: object = {
            "t": DateUtil.now(),
            "code": code,
            "channel": NativeSdkHelper.channel,
            "os": cc.sys.os,
            "phone_type": NativeSdkHelper.getPhoneType(),
            "bundle_name": NativeSdkHelper.getBundleName(),
            "system_version": NativeSdkHelper.getSystemVersion(),
            "udid": NativeSdkHelper.getUDID()
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            UIManager.instance.hideLoadingIcon()
            if (isOK) {
                Logger.log("wechat_auth==", response)
                let code: number = response["code"];
                if (code == ErrorCode.Code.Suc) {
                    let data: object = response["data"];
                    AccountModel.instance.account = data["account"]
                    AccountModel.instance.token = data["token"]
                    AccountModel.instance.save()
                    ServerConfig.initHallServer(data)
                    SocketClient.login(true)
                } else {
                    let msg: string = response["msg"]
                    UIHelper.showTip(msg)
                }
            }
        }, JSON.stringify(param));
    }

    public static get_club_room_statics(clubid: number, time: number, callback: Function) {
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getLogServerUrl() + "/get_club_room_statics"
        let data: object = {
            "t": DateUtil.now(),
            "clubid": clubid,
            "time": time,
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        Logger.log("get_club_room_statics==11", data)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            Logger.log("get_club_room_statics==", response)
            if (isOK) {
                let code: number = response["code"];
                if (code == ErrorCode.Code.Suc) {
                    let data: object = response["data"];
                    callback && callback(data);
                } else {
                    let msg: string = response["msg"]
                    UIHelper.showTip(msg)
                }
            }
        }, JSON.stringify(param));
    }

    public static get_club_in_out_history(clubid: number, callback: Function) {
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getLogServerUrl() + "/get_club_in_out_history"
        let data: object = {
            "t": DateUtil.now(),
            "clubid": clubid,
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        Logger.log("get_club_in_out_history==11", data)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            Logger.log("get_club_in_out_history==", response)
            if (isOK) {
                let code: number = response["code"];
                if (code == ErrorCode.Code.Suc) {
                    let data: object = response["data"];
                    callback && callback(data);
                } else {
                    let msg: string = response["msg"]
                    UIHelper.showTip(msg)
                }
            }
        }, JSON.stringify(param));
    }

    public static get_club_top(clubid: number, topType: number, dateStr: string, page_size: number, cur_page: number, callback: Function) {
        let hadQuanXian: boolean = true
        // if (UserModel.instance.nowClub.user_state == ClubConfig.Club_merber_state_type.freeze) {
        //     hadQuanXian = false
        //     UIHelper.showTip("您已被管理员冻结，没有操作权限")
        // }
        // else {
          
        // }

        if (hadQuanXian) {
            let http: HttpUtil = new HttpUtil();
            let url: string = ServerConfig.getLogServerUrl() + "/get_club_top"
            let data: object = {
                "t": DateUtil.now(),
                "clubid": clubid,
                "topType": topType,
                "dateStr": dateStr,
                "page_size": page_size,
                "cur_page": cur_page,
            }
            let param: object = {
                "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
                "data": data
            }
            http.setMethod(HttpUtil.methodTypeOption.POST)
            Logger.log("get_club_top==11", data)
            http.requestByBody(url, (isOK: boolean, response: any) => {
                Logger.log("get_club_top==", response)
                if (isOK) {
                    let code: number = response["code"];
                    if (code == ErrorCode.Code.Suc) {
                        let data: object = response["data"];
                        callback && callback(data);
                    } else {
                        let msg: string = response["msg"]
                        UIHelper.showTip(msg)
                    }
                }
            }, JSON.stringify(param));
        }
    }

    public static get_club_top_one(clubid: number, userid: number, dateStr: string, callback: Function) {
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getLogServerUrl() + "/get_club_top_one"
        let data: object = {
            "t": DateUtil.now(),
            "clubid": clubid,
            "userid": userid,
            "dateStr": dateStr
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        Logger.log("get_club_top_one==11", data)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            Logger.log("get_club_top_one==", response)
            if (isOK) {
                let code: number = response["code"];
                if (code == ErrorCode.Code.Suc) {
                    let data: object = response["data"];
                    callback && callback(data);
                } else {
                    let msg: string = response["msg"]
                    UIHelper.showTip(msg)
                }
            }
        }, JSON.stringify(param));
    }


    public static get_club_freeze_history(clubid: number, callback: Function) {
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getLogServerUrl() + "/get_club_freeze_history"
        let data: object = {
            "t": DateUtil.now(),
            "clubid": clubid,
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        Logger.log("get_club_freeze_history==11", data)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            Logger.log("get_club_freeze_history==", response)
            if (isOK) {
                let code: number = response["code"];
                if (code == ErrorCode.Code.Suc) {
                    let data: object = response["data"];
                    callback && callback(data);
                } else {
                    let msg: string = response["msg"]
                    UIHelper.showTip(msg)
                }
            }
        }, JSON.stringify(param));
    }


    public static get_give_history(sender: number, reciver: number, page_size: number, cur_page: number, callback: Function) {
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getLogServerUrl() + "/get_give_history"
        let data: object = {
            "t": DateUtil.now(),
            "sender": sender,
            "reciver": reciver,
            "page_size": page_size,
            "cur_page": cur_page
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        Logger.log("get_give_history==11", data)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            Logger.log("get_give_history==", response)
            if (isOK) {
                let code: number = response["code"];
                if (code == ErrorCode.Code.Suc) {
                    let data: object = response["data"];
                    callback && callback(data["data"], data["totalNum"]);
                } else {
                    let msg: string = response["msg"]
                    UIHelper.showTip(msg)
                }
            }
        }, JSON.stringify(param));
    }

    public static req_pay(id: number) {
        // UIHelper.showTip("充值未开放")
        return
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getPayServerUrl() + "/req_pay"
        let data: object = {
            "t": DateUtil.now(),
            "goodid": id,
            "userid": UserModel.instance.userId,
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        Logger.log("req_pay==11", data)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            Logger.log("req_pay==", response)
            if (isOK) {
                let code: number = response["code"];
                if (code == ErrorCode.Code.Suc) {
                    let data: string = response["data"];
                    NativeSdkHelper.pay(data)
                } else {
                    let msg: string = response["msg"]
                    UIHelper.showTip(msg)
                }
            }
        }, JSON.stringify(param));
    }


    public static get_user_data(userid: number, callback: Function) {
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getAccountServerUrl() + "/get_user_data"
        let data: object = {
            "t": DateUtil.now(),
            "userid": userid,
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        Logger.log("get_user_data==11", data)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            Logger.log("get_user_data==", response)
            if (isOK) {
                let code: number = response["code"];
                if (code == ErrorCode.Code.Suc) {
                    let data: string = response["data"];
                    callback && callback(data)
                } else {
                    let msg: string = response["msg"]
                    UIHelper.showTip(msg)
                }
            }
        }, JSON.stringify(param));
    }

}